<script setup lang="ts">
import { createPost } from '@/api/post'
import { uploadImage } from '@/api/upload'
import { computed, ref } from 'vue'

// 表单数据
const title = ref('')
const content = ref('')
const images = ref<string[]>([])

// 表单是否有效
const isValid = computed(() => {
  return title.value.trim().length > 0 && content.value.trim().length > 0
})

// 选择图片
// 添加加载状态
const isSubmitting = ref(false)

// 修改图片选择方法
async function chooseImage() {
  try {
    const res = await uni.chooseImage({
      count: 9 - images.value.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
    })

    uni.showLoading({ title: '上传中' })
    const uploadTasks = res.tempFiles.map(file => uploadImage(file))
    const urls = await Promise.all(uploadTasks)

    images.value = [...images.value, ...urls]
    uni.hideLoading()
  }
  catch (error) {
    console.error(error)
    uni.hideLoading()
    uni.showToast({ title: '上传失败', icon: 'none' })
  }
}

// 重构发布方法
async function publishPost() {
  if (!isValid.value || isSubmitting.value)
    return

  try {
    isSubmitting.value = true
    uni.showLoading({ title: '发布中...' })

    await createPost({
      title: title.value.trim(),
      content: content.value.trim(),
      images: images.value,
    })

    uni.hideLoading()
    uni.showToast({ title: '发布成功', icon: 'success' })

    setTimeout(() => {
      uni.navigateBack()
      isSubmitting.value = false
    }, 1500)
  }
  catch (error) {
    uni.hideLoading()
    isSubmitting.value = false
    uni.showToast({ title: '发布失败', icon: 'none' })
  }
}

// 删除图片
function deleteImage(index: number) {
  images.value.splice(index, 1)
}
</script>

<template>
  <view class="container">
    <view class="form">
      <!-- 标题 -->
      <view class="form-item">
        <input
          v-model="title"
          class="title-input"
          type="text"
          placeholder="请输入标题"
          maxlength="50"
        >
        <view class="word-count">
          {{ title.length }}/50
        </view>
      </view>

      <!-- 内容 -->
      <view class="form-item">
        <textarea
          v-model="content"
          class="content-input"
          placeholder="分享你的想法..."
          maxlength="1000"
        />
        <view class="word-count">
          {{ content.length }}/1000
        </view>
      </view>

      <!-- 图片上传 -->
      <view class="form-item">
        <view class="image-list">
          <view
            v-for="(image, index) in images"
            :key="index"
            class="image-item"
          >
            <image :src="image" mode="aspectFill" />
            <view class="delete-btn" @click="deleteImage(index)">
              <text class="iconfont icon-close" />
            </view>
          </view>

          <view v-if="images.length < 9" class="upload-btn" @click="chooseImage">
            <text class="iconfont icon-camera" />
            <text class="upload-text">
              上传图片
            </text>
          </view>
        </view>
        <view class="upload-tip">
          最多上传9张图片
        </view>
      </view>
    </view>

    <!-- 发布按钮 -->
    <view class="submit-btn" :class="{ disabled: !isValid }" @click="publishPost">
      发布
    </view>
  </view>
</template>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 30rpx;
}

.form {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;

  .form-item {
    position: relative;
    margin-bottom: 30rpx;

    &:last-child {
      margin-bottom: 0;
    }

    .title-input {
      width: 100%;
      height: 80rpx;
      font-size: 32rpx;
      color: #333;
      font-weight: bold;
    }

    .content-input {
      width: 100%;
      height: 400rpx;
      font-size: 28rpx;
      color: #666;
      line-height: 1.6;
    }

    .word-count {
      position: absolute;
      right: 0;
      bottom: 0;
      font-size: 24rpx;
      color: #999;
    }

    .image-list {
      display: flex;
      flex-wrap: wrap;
      margin: 0 -10rpx;

      .image-item {
        position: relative;
        width: calc(33.33% - 20rpx);
        height: 200rpx;
        margin: 10rpx;

        image {
          width: 100%;
          height: 100%;
          border-radius: 8rpx;
        }

        .delete-btn {
          position: absolute;
          right: -10rpx;
          top: -10rpx;
          width: 40rpx;
          height: 40rpx;
          background: rgba(0, 0, 0, 0.5);
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;

          .iconfont {
            font-size: 24rpx;
            color: #fff;
          }
        }
      }

      .upload-btn {
        width: calc(33.33% - 20rpx);
        height: 200rpx;
        margin: 10rpx;
        background: #f8f8f8;
        border-radius: 8rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        .iconfont {
          font-size: 48rpx;
          color: #999;
          margin-bottom: 10rpx;
        }

        .upload-text {
          font-size: 24rpx;
          color: #999;
        }
      }
    }

    .upload-tip {
      font-size: 24rpx;
      color: #999;
      margin-top: 20rpx;
    }
  }
}

.submit-btn {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  text-align: center;
  background: #4caf50;
  border-radius: 44rpx;
  font-size: 32rpx;
  color: #fff;
  margin-top: 60rpx;

  &.disabled {
    background: #ccc;
  }
}
</style>
